package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.*;
import com.miku.blog.domain.dto.WebsiteInfoDTO;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.service.WebsiteInfoService;
import com.miku.blog.mapper.WebsiteInfoMapper;
import com.miku.blog.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
* @author Miku
* @description 针对表【tb_website_info】的数据库操作Service实现
* @createDate 2022-06-08 08:47:10
*/
@Service
public class WebsiteInfoServiceImpl extends ServiceImpl<WebsiteInfoMapper, WebsiteInfo>
    implements WebsiteInfoService{


    @Autowired
    private WebsiteService websiteService;

    @Override
    public WebsiteInfo getWebSiteInfo(Integer webSiteId){
        LambdaQueryWrapper<WebsiteInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WebsiteInfo::getWebsiteId, webSiteId);
        return getOne(queryWrapper);
    }


    @Override
    public ResponseResult getAbout() {
        Website webSite = websiteService.getWebSite();
        WebsiteInfo webSiteInfo = getWebSiteInfo(webSite.getId());
        return ResponseResult.ok().setData(webSiteInfo.getWebsiteAbout());
    }

    @Override
    public ResponseResult editAbout(WebsiteInfoDTO websiteInfoDTO) {
        String content = websiteInfoDTO.getContent();
        if (!StringUtils.hasText(content)){
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }
        LambdaUpdateWrapper<WebsiteInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WebsiteInfo::getWebsiteId,websiteService.getWebSite().getId())
                .set(WebsiteInfo::getWebsiteAbout,content);
        if (!update(updateWrapper)){
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }



    @Override
    public ResponseResult notice() {
        Website webSite = websiteService.getWebSite();
        WebsiteInfo webSiteInfo = getWebSiteInfo(webSite.getId());
        return ResponseResult.ok().setData(webSiteInfo.getWebsiteNotice());
    }

    @Override
    public ResponseResult editNotice(String notice) {
        if (!StringUtils.hasText(notice)){
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }
        LambdaUpdateWrapper<WebsiteInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WebsiteInfo::getWebsiteNotice,notice);
        if (!update(updateWrapper)) {
            return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
        }
        return ResponseResult.ok();
    }


    @Override
    public boolean updateWebsiteInfo(WebsiteInfo websiteInfo) {
        LambdaUpdateWrapper<WebsiteInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WebsiteInfo::getWebsiteId, SystemConstance.WEBSITE_ID);
        return update(websiteInfo,updateWrapper);
    }


    @Override
    public boolean updateWebsocketUrl(String websocketUrl) {
        LambdaUpdateWrapper<WebsiteInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WebsiteInfo::getWebsiteId,SystemConstance.WEBSITE_ID)
                .set(StringUtils.hasText(websocketUrl),WebsiteInfo::getWebsocketUrl,websocketUrl);
        return update(updateWrapper);
    }
}




